package xxx.yyy.zzz.movie;

import org.assertj.core.util.Strings;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.util.List;
import java.util.Objects;

/**
 * https://www.1905.com/mdb/film/xxxx/scenario/?fr=mdbypsy_jq
 */
public class MoviePlotProcessor implements PageProcessor {

//    private Integer movieId;
    private String url;

    private static final String BASE_URL = "https://www.1905.com/mdb/film/%s/scenario/?fr=mdbypsy_jq";

    public MoviePlotProcessor(Integer movieId) {
        Objects.requireNonNull(movieId);
//        this.movieId = movieId;
        this.url = Strings.formatIfArgs(BASE_URL, movieId);
    }

    // 抓取网站的相关配置，包括：编码、抓取间隔、重试次数等
    private Site site = Site.me().setRetryTimes(3).setSleepTime(1000);

    @Override
    public void process(Page page) {
        List<Selectable> list = page.getHtml().css("ul>li.more-infos-plot>p").nodes();

        for (Selectable selectable : list) {
            String content = selectable.xpath("//p/text()").get();
            page.putField("plot", content);
        }
    }

    public Site getSite() {
        return site;
    }

    public String getUrl() {
        Objects.requireNonNull(url);
        return url;
    }

    public static void main(String[] args) {
        MoviePlotProcessor processor = new MoviePlotProcessor(2244288);
        Spider.create(processor)
                .addUrl(processor.getUrl())
                .thread(1)
                .run();
    }
}
